package lightswitch.server.code.sqlhelper.expression.operators.relational;

import java.util.List;

import lightswitch.server.code.object.DataRow;
import lightswitch.server.code.sqlhelper.ExpressionHelper;
import lightswitch.server.code.sqlhelper.expression.Expression;
import lightswitch.server.code.sqlhelper.expression.NormalTable;
import lightswitch.server.code.sqlhelper.statement.select.From;
import lightswitch.server.code.util.TypeOfData;

public class ExistExpression extends Expression {
	private boolean isNot =false;
	private Expression tableExpression;
	public ExistExpression(Expression tb, boolean not,From from)
	{
		tableExpression =tb;
		isNot=not;
		
	}
	public ExistExpression(String tb, boolean not,From from)
	{
		tableExpression =ExpressionHelper.parseExpression(tb, from);
		isNot=not;
		
	}
	
	public Object GetValue(DataRow row, List<From> parentFromList) {
		dataType = TypeOfData.BOOLEAN;
		
		Object right = tableExpression.GetValue(row,parentFromList);
		boolean res = false;
		if (tableExpression.dataType.equals(TypeOfData.TABLE)) {
			NormalTable tb = (NormalTable) right;
			if(tb.dataRowList.size()>0)
				res=true;
			else
				res=false;
			return (isNot==false?res:!res);
		}
		//throw exception
		return false;
		
	}
}
